package com.xucy.springboot.concurrent.tools.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author xucy
 * @Date 2019-07-20 15:37
 * @Description
 **/

public class CyclicBarrierTest3 {


    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier=new CyclicBarrier(4, new Runnable() {
            @Override
            public void run() {
                System.out.println("所有线程释放之前，进行的操作");
                System.out.println(Thread.currentThread().getName()+"这是当前线程");
            }
        });

        for(int i=0;i<3;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":执行等待了");
                    try {
                        Thread.sleep(2000);
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+":等待结束");
                }
            }).start();
        }

        System.out.println("主程序开始等待");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        System.out.println("主程序开始执行");
    }
}
